package com.neuedu.controllers;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.neuedu.model.po.Candidate;
import com.neuedu.model.po.Lw;
import com.neuedu.model.service.VoteService;

import net.sf.json.JSONObject;

@Controller
public class DetailController {
	
	@Autowired
	private VoteService voteService;
	
	@RequestMapping("/candidatesDetails")
	public @ResponseBody Candidate candidatesDetails(int aid,int cid){
		
		voteService.updateCandidate(aid, cid);
		
		Candidate c = voteService.selectCandidateById(cid);
		
		return c;
	}
	
	@RequestMapping("/candidatesVote")
	public @ResponseBody String vote(int aid, int cid,HttpServletRequest request){
		
		String str = "";
		
		if(voteService.canVote(aid)){
			
			String ip = request.getRemoteAddr();
			voteService.updateCandidateVoteInfo(aid, cid, ip);
			str = "{\"result\":\"投票成功\"}";
		}else{
			str = "{\"result\":\"不在活动范围内，不可以投票\"}";
		}
		
		return str;
	}
	
	@RequestMapping("/getGifts")
	public @ResponseBody List<Lw> getGifts(){
		return voteService.selectAllLw();
	}
	
	@RequestMapping("/setGifts")
	public @ResponseBody Candidate setGifts(int aid,int cid,int lwid,int lwcount,HttpServletRequest request){
		
		String openid = request.getRemoteAddr();
//		JSONObject o = (JSONObject) request.getSession().getAttribute("userinfo");
//		String openid = o.getString("openid");
		
		voteService.updateCandidateGifts(aid, cid, lwid, lwcount, openid);
		
		return voteService.selectCandidateById(cid);
	}
	

}
